<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\controller\Admin;
use think\Db;
use think\Cache;
use think\Hook;
use vendor\Channel;

class Index extends Admin {

    /**
     * @title 表单字段演示
     */
    public function form_demo() {

        $albums = [
                [
                'image' => 'https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top-e3b63a0b1b.png',
                'description' => '描述1',
                'listorder' => '1',
            ], [
                'image' => 'https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top-e3b63a0b1b.png',
                'description' => '描述1',
                'listorder' => '2',
            ],
        ];

        $checkbox = [
            1 => '飞机',
            2 => '汽车',
            3 => '火车',
        ];

        $citys = [
            'province' => '北京市',
            'province' => '北京市',
            'province' => '东城区',
        ];
        
        $radio = [
            1 => '飞机',
            2 => '汽车',
            3 => '火车',
        ];

        builder('form')
                ->addItem('albums_id', 'albums', '图集', $albums)
                ->addItem('button', 'button', '按钮', '按钮说明文字', '', '', 'http://www.baidu.com/')
                ->addItem('checkbox', 'checkbox', '多选', $checkbox, '', '', '说明文字')
                ->addItem('citys', 'citys', '省市县区', $citys, '', '', '')
                ->addItem('datetime', 'datetime', '日期选择', '说明文字', '', '', '')
                ->addItem('hidden', 'hidden', '隐藏域', '1', '', '', '')
                ->addItem('image', 'image', '图片', '', '', '', '200x200')
                ->addItem('title', 'input', '输入框<font color="red">*</font>', '', 'lay-verify="required"', '', 'eee')
                ->addItem('input_number', 'input_number', '输入数字<font color="red">*</font>', '', 'lay-verify="required"', '', '只能输入数字')
                ->addItem('input_select', 'input_select', '选择', 'admin/index/tpl_select', 'lay-verify="required"', '', '只能输入数字')
                ->addItem('layedit', 'layedit', 'LAY编辑器')
                ->addItem('p', 'p', '一段文字', '', '', '', '说明文字')
                ->addItem('radio', 'radio', '单选', $radio)                
                ->addItem('select', 'select', '下拉菜单', $radio)
                ->addItem('smeta_json', 'smeta_json', '补充字段', $radio)
                ->addItem('switch', 'switch', '开关', $checkbox)
                ->addItem('textarea', 'textarea', '多行输入', '', '', '', '多行输入')
                ->addItem('umeditor', 'umeditor', '百度编辑器', '', 'style="height: 200px;"', 'simple')                
                ->build();
        return view();
    }

    /**
     * @title 百度编辑器
     */
    public function ueditor() {
        // php 配置信息
        $config = config('baidu');
        // 获取方法
        $action = !empty($_GET['action']) ? trim($_GET['action']) : '';
        // 实例化处理方法
        $handle = new Channel($config);
        // dd($action);
        // 运行
        $response = $handle->dispatcher($action);
        $result = json_encode($response);
        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state' => 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
        exit();
    }

    /**
     * @title 创建静态页面
     * @access protected
     * @htmlfile 生成的静态文件名称
     * @htmlpath 生成的静态文件路径
     * @param string $templateFile 指定要调用的模板文件
     * 默认为空 由系统自动定位模板文件
     * @return string
     *
     */
    protected function _build_html($htmlfile = '', $htmlpath = '', $templateFile = '') {
        $content = $this->fetch($templateFile);
        $htmlpath = !empty($htmlpath) ? $htmlpath : './appTemplate/';
        $htmlfile = $htmlpath . $htmlfile . '.' . config('url_html_suffix');
        $File = new \think\template\driver\File();
        $File->write($htmlfile, $content);
        return $content;
    }

    /**
     * @title 一站生成静态
     */
    public function html() {


        //  $echo = action('Index/Index', ['id' => 5], 'controller');

        $event = controller('Index/Index', 'controller');
        echo $event->index(5); // 输出 update:5
        //     echo $echo;

        exit;


        $this->_build_html();
    }

    /**
     * @title 临时测试
     */
    public function test() {
        
    }

    /**
     * @title 获取模板列表
     */
    public function tpl_select() {

        $dir = APP_DIR . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . APP_THEME . DIRECTORY_SEPARATOR . 'pc' . DIRECTORY_SEPARATOR . 'index' . DIRECTORY_SEPARATOR;
        $lists = glob($dir . "*.php");

        foreach ($lists as $key => $value) {
            $value = str_replace($dir, '', $value);
            $lists[$key] = str_replace('.' . config('template.view_suffix'), '', $value);
        }

        $this->assign('lists', $lists);

        return view();
    }

    /**
     * 数据 转移
     */
    public function test_data() {
        $lists = db('article')->select();
        foreach ($lists as $key => $value) {
            echo strtotime($value['keywords']) . '<br>';
//            db('article')->where('id', $value['id'])->update([
//                'create_time'=>strtotime($value['keywords'])
//            ]);
        }
    }

    /**
     * @title 控制台
     * @return type
     */
    public function index() {


        $admin = admin_is_login();

        $this->assign('admin', $admin);




        // 菜单
        $menus = db('system_menu')->where('status=1')->order('listorder asc')->select();
        // 转化为树型数组
        $menus = node_merge($menus);
        $this->assign('menus', $menus);
        return view();
    }

    public function home() {

        //   $disk_per = Hook::listen('disk');
        //  $this->assign('disk_per', $disk_per);
        //     $member_reg = Hook::listen('member_reg');
        //     $this->assign('member_count_this_month_group_result', $member_reg);
        //   $this->assign('config', $config);

        $this->assign(session('user_auth'));
        return view();
    }

}
